#pragma once

#include <QtSql/QSqlDatabase>

#include "exports.hpp"
#include "exception.hpp"




namespace db
{
	class DB_SCHEMA_API RDBMS;

	/**
	 * Wrapper around QSqlDatabase
	 */
	class DB_SCHEMA_API Connection
	{
	public:

		typedef boost::shared_ptr<Connection>         Ptr;
		typedef boost::shared_ptr<const Connection>   const_Ptr;

	private:

		QSqlDatabase                     m_database;
		boost::shared_ptr<const RDBMS>   m_rdbms;

	public:

		Connection(QSqlDatabase database, boost::shared_ptr<const RDBMS> rdbms);
		virtual ~Connection();



		/**
		 * Tests if this connection is open or not.
		 */
		bool isOpen() const;



		/**
		 * @returns the rdbms implementation for the database behind this connection
		 */
		boost::shared_ptr<const RDBMS> rdbms() const;



		/**
		 * Begins a transaction.
		 * Calls the transaction method of QSqlDatabase.
		 */
		void begin();

		/**
		 * Commits a transaction.
		 * Calls the commit method of QSqlDatabase.
		 */
		void commit();

		/**
		 * Performs a rollback.
		 * Calls the rollback method of QSqlDatabase.
		 */
		void rollback();

		/**
		 * Prepares the given statement for execution.
		 * Throws an exception when the statement could not be prepared.
		 */
		QSqlQuery prepare(QString statement);

		/**
		 * Executes the given sql statement and throws an exception in case it could not be executed.
		 */
		QSqlQuery execute(QString statement);

		/**
		 * Executes the given sql statement but doesn't throw in if the statement could not be executed.
		 * @warning error checking MUST be done manually
		 */
		QSqlQuery try_execute(QString statement);
	};
	///////////////////////////////////////////////////////////////////////////////////////////////
}
///////////////////////////////////////////////////////////////////////////////////////////////////
